﻿using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ttifa.Push
{
    public class PushPayloadEx
    {
        public PushPayload Value;

        public PushPayloadEx()
        {
            Value = new PushPayload();
            Value.platform = Platform.all();
        }

        public PushPayloadEx(JPushPlatform platform)
        {
            var pushPayload = new PushPayload();
            //推送平台
            switch (platform)
            {
                case JPushPlatform.all: pushPayload.platform = Platform.all(); break;
                case JPushPlatform.android: pushPayload.platform = Platform.android(); break;
                case JPushPlatform.ios: pushPayload.platform = Platform.ios(); break;
                case JPushPlatform.android_ios: pushPayload.platform = Platform.android_ios(); break;
                default: pushPayload.platform = Platform.android_ios(); break;
            }

            Value = pushPayload;
        }

        public PushPayloadEx(string platform)
        {
            platform = platform.ToLower();
            var pushPayload = new PushPayload();
            //推送平台
            switch (platform)
            {
                case "all": pushPayload.platform = Platform.all(); break;
                case "android": pushPayload.platform = Platform.android(); break;
                case "ios": pushPayload.platform = Platform.ios(); break;
                case "android_ios": pushPayload.platform = Platform.android_ios(); break;
                default: pushPayload.platform = Platform.android_ios(); break;
            }

            Value = pushPayload;
        }

        public void SetNotification(string platform, string title, string message, Dictionary<string, string> extras)
        {
            var notification = new Notification().setAlert(message);
            if (platform == "all" || platform.Contains("android"))
            {
                AndroidNotification android = new AndroidNotification().setTitle(title).setAlert(message);
                if ((extras != null) && (extras.Count > 0))
                {
                    foreach (string str in extras.Keys)
                    {
                        android.AddExtra(str, extras[str]);
                    }
                }
                notification.setAndroid(android);
            }

            if (platform == "all" || platform.Contains("ios"))
            {
                IosNotification ios = new IosNotification().autoBadge().setAlert(message);
                if (extras != null && extras.Count > 0)
                {
                    foreach (string str in extras.Keys)
                    {
                        ios.AddExtra(str, extras[str]);
                    }
                }

                notification.setIos(ios);
            }

            this.Value.notification = notification;
        }

        public void SetNotification(JPushPlatform platform, string title, string message, Dictionary<string, string> extras)
        {
            var platformName = platform.ToString();
            SetNotification(platformName, title, message, extras);
        }

        public void SetAudience(AudienceEnum type, params string[] values)
        {
            switch (type)
            {
                case AudienceEnum.tag:
                    this.Value.audience = Audience.s_tag(values);
                    break;

                case AudienceEnum.alias:
                    this.Value.audience = Audience.s_alias(values);
                    break;

                case AudienceEnum.registrationId:
                    this.Value.audience = Audience.s_registrationId(values);
                    break;
                default: this.Value.audience = Audience.all(); break;
            }
        }

        public void SetOptions(int sendno, bool production)
        {
            this.Value.options = new Options();
            this.Value.options.sendno = sendno;
            //this.Value.options.time_to_live = 86400;//离线消息保存时间（默认1天）
            this.Value.options.apns_production = production;
        }
    }
}